//
//  CCKYTools.m
//  SwiftHealing
//
//  Created by chenchen on 16/8/19.
//  Copyright © 2016年 ios0610. All rights reserved.
//

#import "CCKYTools.h"

@interface CCKYTools ()

@property (nonatomic, strong) AFHTTPSessionManager* sessionManager;

@end

@implementation CCKYTools

+ (instancetype)sharedTools
{
    static CCKYTools* instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[self alloc] init];
    });
    return instance;
}

#pragma mark - 懒加载
- (AFHTTPSessionManager*)sessionManager
{

    if (_sessionManager == nil) {
        //初始化manager
        _sessionManager = [AFHTTPSessionManager manager];

        //修改manager默认的请求和获取到的数据的格式
        _sessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
        _sessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];

        //设置接收的数据的格式
        NSSet* set = [NSSet setWithObjects:@"text/plain", @"text/html", nil];
//        NSLog(@"%@", set);

        _sessionManager.responseSerializer.acceptableContentTypes = [_sessionManager.responseSerializer.acceptableContentTypes setByAddingObjectsFromSet:set];
    }
    return _sessionManager;
}

#pragma mark - 接口
/**
 *  登录接口
 */
- (void)loginOpertaionWithSuccess:(void (^)(NSDictionary* response))success
{

    NSString* url = @"http://iosapi.itcast.cn/carelinkQuickLogin.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"user_id" : @(11),
        @"true_name" : @"test",
        @"card_number" : @"111",
        @"card_type" : @(11) };

    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", [responseObject class]);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        //反序列化
        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

        NSDictionary* dict = response[@"data"];
        success(dict);
        NSLog(@"%@", [response class]);
        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
}

/**
 *  首页轮播器接口
 */
- (void)bannerWithSuccess:(void (^)(NSArray* response))success
{
    NSString* url = @"http://iosapi.itcast.cn/banners.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"page_size" : @(10),
        @"page" : @(1) };

    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSArray* array = response[@"data"][@"banners"];
        success(array);

        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
}

/**
 *  根据id获取对应的疾病数据 
 */
- (void)searchCI3ListWithCI1_id:(NSInteger)ci1_id withSuccess:(void (^)(NSArray* response))success
{

    NSString* url = @"http://iosapi.itcast.cn/searchCI3List.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"keyword" : @"",
        @"ci1_id" : @(ci1_id),
        @"page_size" : @(15),
        @"page" : @(1) };

    //    __block NSDictionary* dict = [NSDictionary dictionary];

    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        NSArray* array = response[@"data"];
        success(array);
        //        dict = response;

        NSLog(@"reponse:%@", [response class]);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
}

/**
 *  根据疾病等相关信息,匹配符合要求的医生数量
 */
- (void)matchedDoctorCountWithSuccess:(void (^)(NSInteger doctorCount))success
{
    //{"ci1_id":1,"ci2_id":3,"ci3_id":3,"diagnosis_type":0,"is_confirmed":1,"user_id":1000089,"complications":[],"has_diagnosis":2}

    NSString* url = @"http://iosapi.itcast.cn/matchedDoctorCount.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"ci1_id" : @(1),
        @"ci2_id" : @(3),
        @"ci3_id" : @(3),
        @"diagnosis_type" : @(0),
        @"is_confirmed" : @(1),
        @"user_id" : @(1000089),
        @"complications" : [NSArray new],
        @"has_diagnosis" : @(2) };

    //    __block NSInteger doctorCount = 0;

    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

        success([response[@"data"][@"doctor_count"] integerValue]);
        //        NSLog(@"%@",[response[@"data"][@"doctor_count"] class]);
        NSLog(@"类型: %@", [response class]);
        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
    //    return doctorCount;
}

/**
 *  获取并发症的接口
 *
 *  @return 返回保存并发症数据的数组
 */
- (void)complicationListWithSuccess:(void (^)(NSArray* response))success
{

    NSString* url = @"http://iosapi.itcast.cn/complicationList.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"page" : @(1),
        @"page_size" : @(15),
        @"ci2_id" : @(3) };
    //    __block NSDictionary* array = [NSDictionary dictionary];
    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
//        NSLog(@"%@", responseObject);

//        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

//        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(response[@"data"]);
//        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
}

/**
 *  获取医生列表接口
 */
- (void)matchDoctorsWithSuccess:(void (^)(NSArray* response))success
{

    NSString* url = @"http://iosapi.itcast.cn/matchDoctors.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];

    //    {"ci1_id":1,"ci2_id":3,"ci3_id":3,"diagnosis_type":0,"page_size":15,"is_confirmed":1,"user_id":1000089,"complications":[6],"page":1,"has_diagnosis":2}
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"ci1_id" : @(1),
        @"ci2_id" : @(3),
        @"ci3_id" : @(3),
        @"diagnosis_type" : @(0),
        @"is_confirmed" : @(1),
        @"user_id" : @(1000089),
        @"complications" : @[ @"", @"", @"", @"", @"", @"" ],
        @"has_diagnosis" : @(2) };

    //    __block NSArray* array = [NSArray array];
    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(response[@"data"]);
        //        array = response;

        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
    //    return array;
}

/**
 *  获取医生基本信息
 *
 */
- (void)getDoctorInfoWithSuccess:(void (^)(NSDictionary* response))success
{

    NSString* url = @"http://iosapi.itcast.cn/getDoctorInfo.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];

    //"user_id":1000089,"doctor_id":300000315
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"user_id" : @(1000089),
        @"doctor_id" : @(300000315) };
    //    __block NSDictionary* dict = [NSDictionary dictionary];
    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(response[@"data"]);
        //        dict = response;

        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
    //    return dict;
}

/**
 *  匹配到医生后查看医生接诊条件
 */
- (void)doctorReceivingSettingWithSuccess:(void (^)(NSDictionary* response))success
{
    NSString* url = @"http://iosapi.itcast.cn/doctorReceivingSetting.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];

    //"user_id":1000089,"doctor_id":300000315
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"doctor_id" : @(300000315) };

    //    __block NSDictionary* dict = [NSDictionary dictionary];

    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(response[@"data"]);
        //        dict = response;
        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
    //    return dict;
}

/**
 *  获取医生简介接口
 *
 *  @return <#return value description#>
 */
- (void)getIntroductionWithSuccess:(void (^)(NSString* response))success
{

    NSString* url = @"http://iosapi.itcast.cn/getIntroduction.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];

    //"user_id":1000089,"doctor_id":300000315
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"doctor_id" : @(300000315) };
    //    __block NSDictionary* dict = [NSDictionary dictionary];

    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(response[@"data"][@"introduction"]);
        //        dict = response;
        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
    //    return dict;
}

- (void)doctorDutiesWithSuccess:(void (^)(NSDictionary* response))success
{
    //
    NSString* url = @"http://iosapi.itcast.cn/doctorDuties.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];

    //"user_id":1000089,"doctor_id":300000315
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"doctor_id" : @(300000315) };
    //    __block NSDictionary* dict = [NSDictionary dictionary];
    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
//        NSLog(@"%@", responseObject);

//        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

//        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(response[@"data"]);
        //        dict = response;
//        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
    //    return dict;
}

- (void)addDoctorWithSuccess:(void (^)())success
{
    NSString* url = @"http://iosapi.itcast.cn/addDoctor.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];

    //"user_id":1000089,"doctor_id":300000315
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"user_id" : @(1000089),
        @"doctor_id" : @(300000315) };

    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

        success(response[@"data"]);
        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
}

- (void)deleteDoctorWithSuccess:(void (^)(NSDictionary* response))success
{

    NSString* url = @"http://iosapi.itcast.cn/deleteDoctor.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];

    //"user_id":1000089,"doctor_id":300000315
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"user_id" : @(1000089),
        @"doctor_id" : @(300000315) };

    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
        success(response[@"data"]);

        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
}

- (void)doctorListWithSuccess:(void (^)(NSArray* response))success
{

    NSString* url = @"http://iosapi.itcast.cn/doctorList.json.php";
    //header 赋值
    //   [sessionManager.requestSerializer setValue:@"985c96ce43bb5ef69de3e1ad8afaa83f" forHTTPHeaderField:@"login_token"];

    //"user_id":1000089,"doctor_id":300000315
    NSDictionary* paramDic = @{ @"login_token" : @"985c96ce43bb5ef69de3e1ad8afaa83f",
        @"user_id" : @(1000089),
        @"page_size" : @(15),
        @"page" : @(1) };

    [self.sessionManager POST:url parameters:paramDic progress:nil success:^(NSURLSessionDataTask* _Nonnull task, id _Nullable responseObject) {
        NSLog(@"%@", responseObject);

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"str:%@", str);

        NSDictionary* response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

        success(response[@"data"]);
        NSLog(@"reponse:%@", response);

    }
        failure:^(NSURLSessionDataTask* _Nullable task, NSError* _Nonnull error) {
            NSLog(@"%@", error.description);
        }];
}

@end
